package dao;

import javax.swing.*;
import java.sql.*;

/**
 * 链接MySQL基本类.
 *
 * @author 任禹名
 */
public class BaseDao {
    private static String driver = "com.mysql.cj.jdbc.Driver";//数据库驱动
    private static String url = "jdbc:mysql://localhost:3306/gobang";  //url地址
    private static String username = "root";//用户名
    private static String password = "123456";//密码
    private static Connection connection = null;//连接对象
    private static PreparedStatement preparedStatement = null;//sql语句预处理对象
    private static ResultSet resultSet = null;//查询生成的结果集
    private static int rows = 0;//执行增删改sql语句返回的影响的行数

    public static String getDriver() {
        return driver;
    }

    public static void setDriver(String driver) {
        BaseDao.driver = driver;
    }

    public static String getUrl() {
        return url;
    }

    public static void setUrl(String url) {
        BaseDao.url = url;
    }

    public static String getUsername() {
        return username;
    }

    public static void setUsername(String username) {
        BaseDao.username = username;
    }

    public static String getPassword() {
        return password;
    }

    public static void setPassword(String password) {
        BaseDao.password = password;
    }

    /**
     * 创建数据连接
     *
     * @return 返回连接
     */
    public static Connection getConnection() {
        Connection connection = null;
        while (connection == null) {
            try {
                System.out.println(url);
                System.out.println(username);
                System.out.println(password);
                Class.forName(driver);
                connection = DriverManager.getConnection(url, username, password);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                setSQL();
                connection = null;
            }
        }
        return connection;
    }

    public static void setSQL() {
        String port = JOptionPane.showInputDialog("输入MySQL端口号");
        String database = JOptionPane.showInputDialog("输入chessUser表所在数据库名");
        String sqlUserName = JOptionPane.showInputDialog("输入MySQL用户名");
        String password = JOptionPane.showInputDialog("输入MySQL用户密码");
        String url = "jdbc:mysql://localhost:" + port + "/" + database;

        BaseDao.setUrl(url);
        BaseDao.setUsername(sqlUserName);
        BaseDao.setPassword(password);
    }

    /**
     * @param sql 增删改语句
     * @return 返回的数字表示影响了几行
     */
    public static int exeUpdate(String sql) {
        connection = getConnection();
        try {
            preparedStatement = connection.prepareStatement(sql);
            rows = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeResultSet(resultSet);
            closeAll(resultSet);
        }
        return rows;
    }

    /**
     * 执行增删改语句
     *
     * @param sql    查询语句
     * @param params 存放sql语句中?对应的内容
     * @return 返回的数字表示影响了几行
     */
    public static int exeUpdate(String sql, String[] params) {
        connection = getConnection();
        try {
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                preparedStatement.setObject(i + 1, params[i]);
            }
            rows = preparedStatement.executeUpdate();
            if (rows > 0) {
                System.out.println("sql语句执行成功");
            } else {
                System.out.println("sql语句执行失败");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {

            closeResultSet(resultSet);
            closeAll(resultSet);
        }
        return rows;
    }

    /**
     * 执行查询语句
     *
     * @param sql 查询语句
     * @return 执行成功返回结果集
     */
    public static ResultSet exeQuery(String sql) {
        connection = getConnection();
        try {
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultSet;
    }

    /**
     * 执行查询语句
     *
     * @param sql    查询语句
     * @param params 存放sql语句中?对应的内容
     * @return 执行成功返回结果集
     */
    public static ResultSet exeQuery(String sql, String[] params) {
        connection = getConnection();
        try {
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                preparedStatement.setObject(i + 1, params[i]);
            }
            resultSet = preparedStatement.executeQuery();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultSet;
    }

    /**
     * 关闭ResultSet资源
     *
     * @param resultSet
     */
    private static void closeResultSet(ResultSet resultSet) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 关闭资源
     *
     * @param resultSet 结果集
     */
    public static void closeAll(ResultSet resultSet) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (preparedStatement != null) {
                preparedStatement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 释放资源
     *
     * @param resources
     */
    public static void releaseResources(AutoCloseable... resources) {
        for (AutoCloseable resource : resources) {
            if (resource != null) {
                try {
                    resource.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

